package com.crawler.util.contex.impl;

import com.crawler.util.StrKit;
import com.crawler.util.contex.ContentExtractor;
import com.crawler.util.contex.ContentExtractorFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 复杂化的正文抽取器
 *
 * @author guor
 */
public class ComplexContentExtractor implements ContentExtractor {
    private final String expression;

    List<ContentExtractor> extractors = new ArrayList<>();

    public ComplexContentExtractor(String expression) {
        this.expression = expression;
        if (this.getExpression().indexOf(JOIN) != -1) {
            String[] ctx = this.getExpression().split("\\+");
            for (String exp : ctx) {
                extractors.add(ContentExtractorFactory.create(exp));
            }
        }
    }

    @Override
    public String extract(String html) {
        for (ContentExtractor extractor : extractors) {
            String content = extractor.extract(html);
            if (!StrKit.isEmpty(content)) {
                return content;
            }
        }
        return null;
    }

    public String getExpression() {
        return expression;
    }
}
